﻿
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Web;

namespace WeChatParking.Core.Payment.Alipay
{
    public class AlipayHelper
    {
        /// <summary>
        /// 获取签名值
        /// </summary>
        /// <param name="parameters">参数数组</param>
        /// <param name="alipayKey">交易安全检验码</param>
        /// <returns></returns>
        public static string GetSign(Dictionary<string, string> parameters,string alipayKey )
        {
            parameters =PayUtil.FilterPara(parameters);
            parameters.Remove("sign");
            parameters.Remove("sign_type");
            IDictionary<string, string> sortedParams = new SortedDictionary<string, string>(parameters);

            //将Dictionary信息转换成key1=value1&key2=value2的形式
            string stringData = PayUtil.CoverDictionaryToString(sortedParams);
            stringData = stringData + alipayKey;
            string sign = WeChatParking.Util.Security.GetMd5Hash(stringData);

            return sign;
        }

        /// <summary>
        /// 验证签名
        /// </summary>
        /// <param name="parameters">参数数组</param>
        /// <param name="alipayKey">交易安全检验码</param>
        /// <param name="sign">签名值</param>
        /// <returns></returns>
        public static bool CheckSign(Dictionary<string, string> parameters,string alipayKey,string sign)
        {

            string mysign = GetSign(parameters, alipayKey);
            if (mysign == sign)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



    }
}
